layui.use(['layer', 'form', 'table', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var WxOrder = {
        tableId: "wxOrderTable",    //表格id
        condition: {
        	nickname: ""
        }
    };

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });
    /**
     * 初始化表格的列
     */
    WxOrder.initColumn = function () {
        return [[
        	{type: 'checkbox'},
        	{field: 'orderNo',align:'center', title: '订单编号'},
        	{field: 'deviceSerial', sort: true, title: '机柜ID'},
        	{field: 'soltNum',width: 100, sort: true, title: '柜格ID'},
            {field: 'phone', width: 130, sort: true, title: '电话'},
            {field: 'totalFee',width: 100, sort: true, title: '押金'},
            {field: 'status',width: 110, sort: true, title: '状态'},
            {field: 'orderType',width: 110, sort: true, title: '付款类型'},
            {field: 'addTime',width: 160, sort: true, title: '付款时间'},
            {field: 'endTime',width: 160, sort: true, title: '结束时间'},
            {field: 'useTime', width: 110, sort: true, title: '使用时长'},
            {field: 'amount', width: 80, sort: true, title: '盈利'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 100}
        ]];
    };

    /**
     * 点击查询按钮
     */
    WxOrder.search = function () {
        var queryData = {};
        queryData['phone'] = $("#phone").val();
        queryData['status'] = $("#status").val();
        queryData['deviceSerial'] = $("#deviceSerial").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        table.reload(WxOrder.tableId, {where: queryData, page:{curr: 1}});
    };

    WxOrder.settlement = function (data) {
    	if(data.endTime != null && data.endTime != ""){
    		Feng.success("该订单已完成");
    	}else{
    		var operation = function () {
    			var ajax = new $ax(Feng.ctxPath + "/wx_order/settlement", function () {
    				table.reload(WxOrder.tableId);
    				Feng.success("强制结算成功!");
    			}, function (data) {
    				Feng.error("强制结算失败!" + data.responseJSON.message + "!");
    			});
    			ajax.set("orderNo", data.orderNo);
    			ajax.start();
    		};
    		Feng.confirm("强制结算将扣除该用户余额：" + data.totalFee + "。<br>是否结算订单： " + data.orderNo + " ?", operation);
    	}
    }

    /**
     * 导出excel按钮
     */
    WxOrder.exportExcel = function () {
        var checkRows = table.checkStatus(WxOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + WxOrder.tableId,
        url: Feng.ctxPath + '/wx_order/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        limit:15,
        limits: [15,30,50,100],
        cols: WxOrder.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        WxOrder.search();
    });
    // 导出excel
    $('#btnExp').click(function () {
    	WxOrder.exportExcel();
    });
    // 工具条点击事件
    table.on('tool(' + WxOrder.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'settlement') {
        	WxOrder.settlement(data);
        }
    });
});
